<div class="panelBar">
	<ul class="toolBar">
		<li><a class="add" href="javascript:;" url="entity/cg_project/add_pay.html?entity_id={entity_id}&&entity={entity}" target="dialog" rel="addPay" mask="true" callback="pay" title="添加支付信息" width="500" height="300"><span>添加支付明细</span></a></li>
		<li><a class="delete" url="/pay/{pay_id}" area="tbody.list" callback="del" type="delete" target="navTabTodo" title="确定要删除选中的数据吗？" warn="请选择需要删除的数据项！"><span>删除</span></a></li>
	</ul>
</div>
<div class="tableList">
	<table class="list" width="100%">
		<thead>
			<tr>
				<th width="25"><input type="checkbox" name="objectId" value="${pay_id}"/></th>
				<th width="100">第*次支付</th>
				<th >支付日期</th>
				<th >支付金额</th>
				<th width="50">操作</th>
			</tr>
		</thead>
		<tbody class="templet">
			<tr entity_id="${entity_id}" put="/pay/${pay_id}">
				<td><input type="checkbox" name="pay_id" value="${pay_id}" /></td>
				<td field="ord" align="center">${ord}</td>
				<td field="paydate" validator="date" align="center">${paydate}</td>
				<td field="payprice" validator="number" format="#,###.##" align="right">${payprice}</td>
				<td align="center">
					<a url="entity/cg_project/update_pay.html?pay_id=${pay_id}&entity_id={entity_id}&entity={entity}" target="dialog" width="450" height="300" maxable="false"  callback="setTotalPay" title="修改支付明细"
						onclick="var $this=$(this);$this.attr('href',$this.attr('url'));" title="修改支付明细"><img src="themes/default/images/form/update.gif"></a>
				</td>
			</tr>
		</tbody>
		<tbody class="list" action="{entity}/{entity_id}/pay">
		</tbody>
	</table>
	<p>
		<font color="red">*支付明细变更后，请刷新页面更新“指定支付年份”和“年内支付金额”信息</font>
	</p>
</div>
<script>

//删除支付信息的回调函数
function del(data,jDom){
	var tb = jDom.parents('.panelBar').next().find('>table.list');
	jDom.parents('.panelBar').next().find('>table.list').removeAttr('loaded').jBinds(
		function(data){updateEntityExt($("tbody.list",tb));}		
	);
}

//新增支付信息的回调函数
function pay(data,jDom){
	if(!data||data.length==0) return;
	var tb = jDom.parents('.panelBar').next().find('>table.list');
	var url = $("tbody.list",tb).attr("action");
	DWZ.get(url,function(){
		tb.removeAttr('loaded').jBinds(function(data){updateEntityExt($("tbody.list",tb));});
	});
}

//更新entity列表的总支付,最近支付日期信息和年支付金额
function updateEntityExt(bodyDom){
	var $tr = bodyDom.find("tr[put]");
	var totalPrice = 0;
	var lastDate = "0000-00-00";
	var yearInfo="";
	$.each($tr,function(i,p){
		var price = $(p).find("td[field=payprice]").text();
		var date = $(p).find("td[field=paydate]").text();
		totalPrice = totalPrice + parseInt(price);
		lastDate = parseInt(lastDate.replaceAll("-",""))<parseInt(date.replaceAll("-",""))?date:lastDate;
		
		//年支付统计
		var year = date.substring(0,4);
		if(yearInfo==""){
			yearInfo = year+"_"+price+" ";
		}
		else if(yearInfo.indexOf(year+"_")<0){
			yearInfo = yearInfo + year+"_"+price + " ";
		}
		else{
			var tempStr = yearInfo.substring(yearInfo.indexOf(year+"_"),yearInfo.length); 
			var yearPrice = tempStr.substr(0,tempStr.indexOf(" ")); //eg.yearPrice:2013_87.834
			var oldPrice = yearPrice.substr(5,yearPrice.length);
			var newPrice = parseInt(oldPrice) + parseInt(price);
			yearInfo = yearInfo.replace(yearPrice,year+"_"+newPrice);
		}
	});
	var entityId = $tr.first().attr("entity_id");
	var $entity = $("tr[entity="+entityId+"]");
	var totalCol = $("td[name=number1]",$entity);
	var lastDateCol = $("td[name=date1]",$entity);
	var yearPriceCol = $("td[name=str22]",$entity);
	if(typeof(totalCol)!=undefined){
		totalCol.text(totalPrice.format("#,###.##"));
	}
	if(typeof(lastDateCol!=undefined)){
		lastDateCol.text(lastDate);
	}
	if(typeof(yearPriceCol!=undefined)){
		yearPriceCol.text(yearInfo);
	}
}

/**
 * 回调函数,更新支付明细数据
 */
function setTotalPay(data,jDom){
	var trRow = jDom.parents("tr[put]");
	trRow.find("td[field=ord]").text(data.ord);
	trRow.find("td[field=payprice]").text(data.payprice);
	trRow.find("td[field=paydate]").text(data.paydate);
	
	updateEntityExt(trRow.parent("tbody"));
}
</script>